'''
Created on Oct 13, 2012

@author: raygomez
'''

from AVP import AVP
from codecs import getencoder
from codecs import getdecoder

utf8Encoder = getencoder("utf_8")
utf8Decoder = getdecoder("utf_8")

class UTF8StringAVP(AVP):
    """AVP with UTF-8 string payload."""
    
    def __init__(self, code, value="", vendorId=0):
        AVP.__init__(self, code, utf8Encoder(value)[0], vendorId) 
        
    def getValue(self):
        """Returns the payload as a string (possible a unicode string)"""
        return utf8Decoder(self.payload)[0]
    
    def setValue(self, value):
        self.payload = utf8Encoder(value)[0]
        
    def __str__(self):
        return str(self.code) + ":" + str(self.getValue())
    
    
    @staticmethod
    def convert(avp):
        """Convert generic AVP to UTF8StringAVP"""
        a = UTF8StringAVP(avp.code, vendorId = avp.vendorId)
        a.flags = avp.flags
        a.payload = a.payload
        return a
